/*
 * @公共函数
 */


const imgUrl = process.env.VUE_APP_API_IMG_URL === '/' ? '' : process.env.VUE_APP_API_IMG_URL
/**
 * @description:  中国时间转 yyyy-mm-dd 格式
 * @param  obj
 * @return {string}
 */
export function fmtDate(obj) {
	var date = new Date(obj);
	var y = 1900 + date.getYear();
	var m = "0" + (date.getMonth() + 1);
	var d = "0" + date.getDate();
	return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
}   //中国时间转成 yyyy-mm-dd 时间




export function imageUrl(url) {
	return `${imgUrl}${url}`
}

/**
 * @description: 获取当前时间
 * @return {string}
 */
export function sysTime() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = myDate.getMonth() + 1;
	var date = myDate.getDate();
	var h = myDate.getHours();
	var m = myDate.getMinutes();
	var s = myDate.getSeconds();
	var now =
		year +
		"-" +
		(month < 10 ? "0" : "") +
		month +
		"-" +
		(date < 10 ? "0" : "") +
		date +
		" " +
		(h < 10 ? "0" : "") +
		h +
		":" +
		(m < 10 ? "0" : "") +
		m +
		":" +
		(s < 10 ? "0" : "") +
		s;
	return now;
}




/**
 * @description: 检查邮箱正则表达式
 * @param {*}
 * @return {*}
 */
export let checkEmail = (rule, value, callback) => {
	// clearTimeout(checkPending);

	if (typeof value != 'string' || value == '') {

		return callback();
	}

	if (!(/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value))) {
		return callback(new Error('邮箱格式错误'));
	} else {
		callback();
	}

}

/**
 * @description: 检查电话正则表达式
 * @param {*}
 * @return {*}
 */
export let checkPhone = (rule, value, callback) => {
	// clearTimeout(checkPending);
	// console.log(typeof value);
	if (typeof value != 'string' || value == '') {

		return callback();
	}
	if (!(/^1[3456789]\d{9}$/.test(value))) {
		return callback(new Error('电话号码格式错误'));
	} else {
		callback();
	}

}


/**
 * @description: 检查用户名正则表达式
 * @param {*}
 * @return {*}
 */
export let checkUserName = (rule, value, callback) => {
	// clearTimeout(checkPending);
	if (typeof value != 'string' || value == '') {

		return callback();
	}
	if (new RegExp("[^\\w]").test(value)) {
		return callback(new Error('只能由字母数字下划线组成'));
	} else {
		callback();
	}

}

/**
 * @description: 检查密码正则表达式
 * @param {*}
 * @return {*}
 */
export let checkPassWord = (rule, value, callback) => {
	// clearTimeout(checkPending);
	if (typeof value != 'string' || value == '') {

		return callback();
	}
	if (new RegExp("[^\\w]").test(value)) {
		return callback(new Error('只能由字母数字下划线组成'));
	} else {
		callback();
	}

}


/**
 * @description: 检查排序非0开头正整数正则表达式
 * @param {*}
 * @return {*}
 */
export let checkNumber = (rule, value, callback) => {

	if (/(^0{1}$)|(^[1-9][0-9]*$)/.test(value)) {
		callback();
	} else {
		return callback(new Error('只能为正整数'));
	}
}

/**
 * @description: 检查价钱
 * @param {*}
 * @return {*}
 */
export let checkPrice = (rule, value, callback) => {

	var reg1 = /(^[0-9]{1}$)|(^[1-9][0-9]{1,}$)|(^[0]{1}[\.]{1}[0-9]{1,2}$)|(^[1-9]{1}[0-9]{0,}[\.]{1}[0-9]{1,2}$)/;;
	if (!reg1.test(value)) {
		return callback(new Error('只能为数字最多两位小数'));
	} else {
		callback();

	}

}

